package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyShengYangJuYing;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 * 北京/沈阳-200/160-聚赢-公积金
 * @ Author 江小游
 * @ Date 2025/6/19 17:09
 */
@ApiPlan(productId = "258", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5ShengYangJuYing implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataChangeMessage) throws Exception {
        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobile", apiDataChangeMessage.getPhoneMd5());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyMessage) throws Exception {
        OrgApplyShengYangJuYing orgApply = new OrgApplyShengYangJuYing();
        // 扩展
        com.ts.api.module.api.convert.core.utils.ApiFieldConverter apiFieldConverter = com.ts.api.module.api.convert.core.utils.ApiFieldConverter.create(apiApplyMessage);
        orgApply.setUsername(apiApplyMessage.getUserName());
        orgApply.setMobile(apiApplyMessage.getPhone());
        orgApply.setCity(apiApplyMessage.getCity());
        orgApply.setAge(apiApplyMessage.getAge());
        // 我方字典-贷款金额
        orgApply.setQuota(String.valueOf(apiFieldConverter.getLoanAmountInt()));
        //  性别 1男 2女
        String sex = ChooseEq.create(apiApplyMessage.getSex())
                .when("0", "1")
                .when("1", "2")
                .end("1");
        orgApply.setSex(sex);
        // 房产情况
        String realEstateInfo = ChooseEq.create(apiApplyMessage.getRealEstateInfo())
                .when(apiApplyMessage.getRealEstateInfo(), "1", "无房产")
                .when(apiApplyMessage.getRealEstateInfo(), "2", "有房不抵押")
                .when(apiApplyMessage.getRealEstateInfo(), "3", "有房可抵押")
                .when(apiApplyMessage.getRealEstateInfo(), "4", "有房产")
                .when(apiApplyMessage.getRealEstateInfo(), "5", "有按揭房产")
                .when(apiApplyMessage.getRealEstateInfo(), "6", "有全款房产")
                .end();
        orgApply.setHouse(realEstateInfo);
        // 车辆情况
        String carInfo = ChooseEq.create(apiApplyMessage.getCarInfo())
                .when(apiApplyMessage.getCarInfo(), "1", "无车产")
                .when(apiApplyMessage.getCarInfo(), "2", "有车不抵押")
                .when(apiApplyMessage.getCarInfo(), "3", "有车可抵押")
                .when(apiApplyMessage.getCarInfo(), "4", "有车")
                .end();
        orgApply.setCar(carInfo);
        // 信用情况（逾期）
        String overdue = ChooseEq.create(apiApplyMessage.getOverdue())
                .when("1", "无逾期")
                .when("2", "当前有逾期")
                .when("3", "一年以内无逾期")
                .end("无逾期");
        orgApply.setCredit(overdue);
        // 公积金
        String providentFund = ChooseEq.create()
                .when(apiApplyMessage.getProvidentFund(), "1", "无公积金")
                .when(apiApplyMessage.getProvidentFund(), "2", "缴纳6个月以下")
                .when(apiApplyMessage.getProvidentFund(), "3", "缴纳6个月以上")
                .when(apiApplyMessage.getProvidentFund(), "4", "有公积金")
                .end("无公积金");
        orgApply.setIsGjj(providentFund);
        // 是否有备注
        StringBuilder stringBuilder = new StringBuilder();
        //   保险保单
        if (ValidParam.isNotEmpty(apiApplyMessage.getInsurancePolicy())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getInsurancePolicy(), "1", "无保险保单")
                    .when(apiApplyMessage.getInsurancePolicy(), "2", "缴纳未满1年")
                    .when(apiApplyMessage.getInsurancePolicy(), "3", "缴纳1年以上")
                    .when(apiApplyMessage.getInsurancePolicy(), "4", "有保险保单")
                    .end("无保险保单");
            stringBuilder.append("保险保单：").append(text).append(";").append("\n");
        }
        if (ValidParam.isNotEmpty(apiApplyMessage.getSesameCredit())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getSesameCredit(), "1", "600分以下")
                    .when(apiApplyMessage.getSesameCredit(), "2", "600~649分")
                    .when(apiApplyMessage.getSesameCredit(), "3", "650~699分")
                    .when(apiApplyMessage.getSesameCredit(), "4", "700分以上")
                    .end("600分以下");
            stringBuilder.append("芝麻分：").append(text).append(";").append("\n");
        }
        // 社保缴纳
        if (ValidParam.isNotEmpty(apiApplyMessage.getSocialSecurity())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getSocialSecurity(), "1", "无社保")
                    .when(apiApplyMessage.getSocialSecurity(), "2", "缴纳6个月以下")
                    .when(apiApplyMessage.getSocialSecurity(), "3", "缴纳6个月以上")
                    .when(apiApplyMessage.getSocialSecurity(), "4", "有社保")
                    .end("无社保");
            stringBuilder.append("社保缴纳：").append(text).append(";").append("\n");
        }
        // 营业执照注册年限
        if (ValidParam.isNotEmpty(apiApplyMessage.getBizLicenseYear())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getBizLicenseYear(), "1", "未注册")
                    .when(apiApplyMessage.getBizLicenseYear(), "2", "注册1年以下")
                    .when(apiApplyMessage.getBizLicenseYear(), "3", "注册1~5年")
                    .when(apiApplyMessage.getBizLicenseYear(), "4", "注册5年以上")
                    .end();
            stringBuilder.append("营业执照：").append(text).append(";").append("\n");
        }
        orgApply.setRemark(stringBuilder.toString());
        return JsonUtils.toJson(orgApply);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
